//
//  YXYNextViewController.m
//  Topit
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYNextViewController.h"
#import "YXYNextTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "YXYNextReplyTableViewCell.h"
#import "MJRefresh.h"
@interface YXYNextViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, copy) NSString *url;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) int number;
@end

@implementation YXYNextViewController

-(void)dealloc {
    [_url release];
    [_array release];
    [_dataSource release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray arrayWithCapacity:0];
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    //楼主Model
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[YXYNextTableViewCell class] forCellReuseIdentifier:@"YXYNextTableViewCellIdentifier"];
    [_tableView registerClass:[YXYNextReplyTableViewCell class] forCellReuseIdentifier:@"YXYNextReplyTableViewCellIdentifier"];
    [self.view addSubview:_tableView];
    [_tableView release];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.mode = MBProgressHUDModeIndeterminate;
    [YXYNetWorkTool getWithUrl:_url parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NextModel *nextModle = [[NextModel alloc] initWithDictionary:result[@"info"][@"sbj"]];
        [_array addObject:nextModle];
        [nextModle release];
        for (NSDictionary *dic in result[@"item"]) {
            YXYReplyModel *replyModel = [[YXYReplyModel alloc] initWithDictionary:dic];
            [_dataSource addObject:replyModel];
            [replyModel release];
        }
        [hud hide:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    } fail:^(NSError *error) {
        [hud hide:YES];
        [ProgressHUD showError:@"请检查网络"];
    }];
    [self getFootRefresh];
        // Do any additional setup after loading the view.
}

- (instancetype)initWithNextUrl:(NSString *)url
{
    self = [super init];
    if (self) {
        self.url = url;
    }
    return self;
}
-(void)getFootRefresh {
    [self.tableView addFooterWithCallback:^{
        self.number += 30;
        NSString *num = [NSString stringWithFormat:@"%d", _number];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:num, @"offset", nil];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"正在加载";
        hud.mode = MBProgressHUDModeIndeterminate;
        [YXYNetWorkTool getWithUrl:self.url parameter:dic httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
            if ([result[@"item"] isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dic in result[@"item"]) {
                    YXYReplyModel *replyModel = [[YXYReplyModel alloc] initWithDictionary:dic];
                    [_dataSource addObject:replyModel];
                    [replyModel release];
                }
                [hud hide:YES];
            }else {
                UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"亲爱的" message:@"已经到底er了" preferredStyle:UIAlertControllerStyleAlert];
                [alertView addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                }]];
                [self presentViewController:alertView animated:YES completion:nil];
            }
            [hud hide:YES];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_tableView reloadData];
                [_tableView footerEndRefreshing];
            });
        } fail:^(NSError *error) {
            [hud hide:YES];
            [ProgressHUD showError:@"请检查网络"];
        }];
    }];
    
}


#pragma mark - TableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count + _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YXYNextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXYNextTableViewCellIdentifier"];
    if (indexPath.row == 0) {
        if (indexPath.row < _array.count) {
            [cell setNextModel:_array[indexPath.row]];
        }
        return cell;
    }else {
        YXYNextReplyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXYNextReplyTableViewCellIdentifier"];
        [cell setReplyModel:_dataSource[indexPath.row - 1]];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        YXYNextTableViewCell *cell = (YXYNextTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        [cell layoutSubviews];
        return [cell getHeight];
    }
    else if (indexPath.row >= 1){
        YXYNextReplyTableViewCell *cell = (YXYNextReplyTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        [cell layoutSubviews];
        return [cell getReplyHeigth];
    }
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
